package com.hht.checkversion;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Author: chenhu
 * Time: 2020/7/2 11:35
 * Description do somethings
 */
public class PackageUtil {

    public PackageUtil() {
    }

    private static class PackageHolder {
        private static PackageUtil packageUtil = new PackageUtil();
    }

    public static PackageUtil getInstance() {
        return PackageHolder.packageUtil;
    }

    /**
     * 获取安装的apk,不包含系统的service应用
     */
    public List<AppBean> getInstalledAppInfo(Context context) {
        PackageManager pm = context.getPackageManager();
        final Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
        Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm));
        List<AppBean> mAppBeanList = new ArrayList<>();

        for (ResolveInfo resolveInfo : resolveInfos) {
            try {
                String packageName = resolveInfo.activityInfo.packageName;//获取包名
                String versionInfo = context.getPackageManager().getPackageInfo(
                        packageName, 0).versionName;
                String appName = resolveInfo.loadLabel(context.getPackageManager()).toString();
                AppBean bean = new AppBean(packageName, versionInfo, appName, getAppIcon(context, packageName));
                mAppBeanList.add(bean);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return mAppBeanList;
    }

    /**
     * 获取系统所有apk,包含系统的service应用
     */
    public List<AppBean> getAllAppInfo(Context context) {
        List<AppBean> mAppBeanList = new ArrayList<>();
        List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(0);//获取安装程序的包名
        for (int i = 0; i < packs.size(); i++) {
            try {
                PackageInfo packageInfo = packs.get(i);//某个包信息
                String packageName = packageInfo.packageName;//获取包名
                String versionInfo = packageInfo.versionName;
                String appName = (String) context.getPackageManager().getApplicationInfo(packageName, 0).
                        loadLabel(context.getPackageManager());
                AppBean bean = new AppBean(packageName, versionInfo, appName, getAppIcon(context, packageName));
                if (!"8.0.0".equals(versionInfo)) {
                    mAppBeanList.add(bean);
                }

            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return mAppBeanList;
    }


    private Drawable getAppIcon(Context mContext, String packname) {
        try {
            PackageManager pm = mContext.getPackageManager();
            ApplicationInfo info = pm.getApplicationInfo(packname, 0);
            return info.loadIcon(pm);
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return mContext.getDrawable(R.mipmap.ic_launcher);
    }
}
